/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2025，所有权利保留。
 * <p>
 * 项目名： cdx-ai-agent
 * 文件名： MyCustomAdvisor.java
 * 模块说明：
 * 修改历史：
 * 2025年09月23日 - chendongxu - 创建。
 */
package
        com.cdx.cdxaiagent.advisor;

import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
import org.springframework.ai.chat.client.advisor.api.AdvisedResponse;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisorChain;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisorChain;

/**
 * @author chendongxu
 * @since 1.0
 */
public class MyCustomAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        // 1. 处理请求（前置处理）
        AdvisedRequest modifiedRequest = processRequest(advisedRequest);

        // 2. 调用链中的下一个Advisor
        AdvisedResponse response = chain.nextAroundCall(modifiedRequest);

        // 3. 处理响应（后置处理）
        return processResponse(response);
    }

    private AdvisedResponse processResponse(AdvisedResponse response) {
        return response;
    }

    private AdvisedRequest processRequest(AdvisedRequest advisedRequest) {
        return advisedRequest;
    }


    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        // 1. 处理请求
        AdvisedRequest modifiedRequest = processRequest(advisedRequest);

        // 2. 调用链中的下一个Advisor并处理流式响应
        return chain.nextAroundStream(modifiedRequest)
                .map(response -> processResponse(response));
    }


    @Override
    public String getName() {
        return "鱼皮自定义的 Advisor";
    }

    @Override
    public int getOrder() {
        return 11;
    }
}
